using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
namespace MemoryGame
{
    public class StateIngameMenu : DEF
    {

        public static Texture2D splashBitmap = null;
        public const int MENU_PLAY = 0;
        public const int MENU_RESTART = 1;
        public const int MENU_OPTION_SOUND = 2;
        public const int MENU_CREDIT = 3;        
        public const int MENU_EXIT = 4;
        public const int MENU_RESUME = 5;
        public const int MENU_BACK_TO_MAINMENU = 6;
        public static int[] arrayMenu = { MENU_RESUME, MENU_RESTART, MENU_BACK_TO_MAINMENU, MENU_EXIT };
        public static String[] arrayMenuString = { "RESUME", "RESTART",  "MAIN MENU" ,"EXIT"};
        public static int MENU_BEGIN_X = SCREEN_WIDTH / 2;
        public static int MENU_BEGIN_Y = 0;
        public static int MENU_ELEMENT_W = 0;
        public static int MENU_ELEMENT_H = 0;
        public static int MENU_ELEMENT_SPACE = 15;
        public static int MENU_BACKGROUND_W = 0;
        public static int MENU_BACKGROUND_H = 0;
        public static int MENU_H = SCREEN_HEIGHT;
        public static Rectangle rectF;

        public static void SendMessage(int type)
        {

            switch (type)
            {
                case MESSAGE_CTOR:
                    SoundManager.pausesoundLoop(1);
                    MENU_BACKGROUND_W = SCREEN_WIDTH / 4 * 3;
                    MENU_BACKGROUND_H = SCREEN_HEIGHT / 6 * 5;
                    MENU_ELEMENT_H = (int)(texttureButtonMenu[DEF.FRAME_BUTTON_NORMAL].Height * Game1.SCALE_Y);
                    MENU_ELEMENT_W = (int)(texttureButtonMenu[DEF.FRAME_BUTTON_NORMAL].Width * Game1.SCALE_X);
                    MENU_ELEMENT_SPACE = (MENU_BACKGROUND_H - ((arrayMenu.Length + 1) * MENU_ELEMENT_H)) / arrayMenu.Length;
                    if (MENU_ELEMENT_SPACE < 5)
                        MENU_ELEMENT_SPACE = 5;
                    MENU_H = (MENU_ELEMENT_SPACE + MENU_ELEMENT_H) * arrayMenu.Length;
                    MENU_BEGIN_X = (SCREEN_WIDTH - MENU_ELEMENT_W) / 2;
                    MENU_BEGIN_Y = (SCREEN_HEIGHT - MENU_H) / 2 + MENU_ELEMENT_H / 4;

                    rectF = new Rectangle((SCREEN_WIDTH - MENU_BACKGROUND_W) / 2, (SCREEN_HEIGHT - MENU_BACKGROUND_H) / 2, MENU_BACKGROUND_W, MENU_BACKGROUND_H);
                    break;
                case MESSAGE_UPDATE:
                    int temp = -1;
                    if (Dialog.isShowDialog)
                    {
                        Dialog.updateDialog();
                        if (InputState.isBackKeyPress())
                        {
                            Dialog.hideDialog();
                        }
                        return;
                    }
                    if (InputState.isBackKeyPress())
                    {
                        SoundManager.playsoundLoop(1, true);
                        Game1.changeState(Game1.STATE_GAMEPLAY, false, true);

                        break;
                    }
                    for (int i = 0; i < arrayMenu.Length; i++)
                    {
                        int y = MENU_BEGIN_Y + i * (MENU_ELEMENT_H + MENU_ELEMENT_SPACE);
                        if (InputState.isTouchReleaseInRect(MENU_BEGIN_X, y, MENU_ELEMENT_W, MENU_ELEMENT_H))
                        {

                           
                            switch (arrayMenu[i])
                            {
                                case MENU_RESUME:
                                    SoundManager.playsoundLoop(1, true);
                                    Game1.changeState(Game1.STATE_GAMEPLAY, false, true);
                                    break;
                                case MENU_RESTART:
                                    Dialog.showDialog(Dialog.DIALOG_TYPE_CONFIRM, "", "Restart game??", Dialog.DIALOG_RESTART_GAME);
                                    break;
                                case MENU_OPTION_SOUND:
                                    Game1.isEnableSound = !Game1.isEnableSound;
                                    if(Game1.isEnableSound)
                                        MediaPlayer.Stop(); 
                                    //SoundManager.playSound(SoundManager.SOUND_SELECT, 1);
                                    //	changeState(STATE_OPTION, true, true);
                                    break;
                                case MENU_BACK_TO_MAINMENU:
                                    Dialog.showDialog(Dialog.DIALOG_TYPE_CONFIRM, "", "Back to Main Menu?", Dialog.DIALOG_BACK_TO_MAINMENU_EXIT);
                                    break;
                                case MENU_EXIT:
                                    Dialog.showDialog(Dialog.DIALOG_TYPE_CONFIRM, "", "DO YOU WANT TO EXIT???", Dialog.DIALOG_NEXTSTATE_EXIT);
                                    break;
                                default:
                                    break;
                            }
                            SoundManager.playSound(SoundManager.SOUND_SELECT);
                        }
                    }

                    break;
                case MESSAGE_PAINT:

                    StateGameplay.SendMessage(MESSAGE_PAINT);
                    if (Dialog.isShowDialog)
                    {
                        Dialog.drawDialog(Game1.spriteBatch);
                        return;
                    }
                    var t = new Texture2D(Game1.graphicsDevice, 1, 1);
                    t.SetData(new[] { Color.White });
                    Game1.spriteBatch.Draw(t, rectF, new Color(0, 0, 0, 200));
                    string text = "";
                    for (int i = 0; i < arrayMenu.Length; i++)
                    {
                        int y = MENU_BEGIN_Y + i * (MENU_ELEMENT_H + MENU_ELEMENT_SPACE);
                        if (InputState.isTouchDrapInRect(MENU_BEGIN_X, y, MENU_ELEMENT_W, MENU_ELEMENT_H))
                            Game1.spriteBatch.Draw(texttureButtonMenu[DEF.FRAME_BUTTON_HIGHTLIGHT], new Vector2(MENU_BEGIN_X, y), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);
                        else
                            Game1.spriteBatch.Draw(texttureButtonMenu[DEF.FRAME_BUTTON_NORMAL], new Vector2(MENU_BEGIN_X, y), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);
                        text = arrayMenuString[i];
                        if (arrayMenu[i] == MENU_OPTION_SOUND)
                        {
                            if (Game1.isEnableSound)
                                text += "ON";
                            else
                                text += "OFF";
                        }

                        Vector2 tempMeasure = Game1.fontNormal.MeasureString(text);
                        Game1.spriteBatch.DrawString(Game1.fontNormal, text, new Vector2(MENU_BEGIN_X + (MENU_ELEMENT_W - tempMeasure.X) / 2 , y + (MENU_ELEMENT_H - tempMeasure.Y) / 2), Color.White);
                    }

                    //if(System.currentTimeMillis()%1000 > 500)
                    //if (fontbig_White != null)
                    //	fontbig_White.drawString(mainCanvas, "TOUCH THE SCREEN TO CONTINUE", SCREEN_WIDTH/2, SCREEN_HEIGHT - 150, BitmapFont.ALIGN_CENTER);	

                    break;
                case MESSAGE_DTOR:
                    InputState.resetTouch();
                    break;
            }
        }
    }
}